package com.oncecloud.devops.client.controller;


import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.oncecloud.devops.client.entity.Role;
import com.oncecloud.devops.client.entity.RoleAction;
import com.oncecloud.devops.client.entity.User;
import com.oncecloud.devops.client.entity.UserRoleEnv;
import com.oncecloud.devops.client.service.RoleService;
import com.oncecloud.devops.client.util.Result;


/**
 * 用户信息控制器
 * @author xzg
 *
 */
@RestController
@RequestMapping("/role")
public class RoleUserController {
	@Resource
	private RoleService roleService;
	
	@RequestMapping(value = "/addRoleForUser", method = { RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Result addRoleForUser(@RequestBody List<UserRoleEnv> listURE){
		int i =	roleService.addUserRole(listURE);
		if(0 == i)
			return new Result(false, "为用户添加角色失败！");
		else
			return new Result(true, "为用户添加角色成功！");
	}
	
	/**
	 * 查看可修改角色
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/getUnderRoles", method = { RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Result getUnderRoles(User user){
		List<Role> list = roleService.getRolePermmitMod(user);
		if(null == list || 0 == list.size())
			return new Result(false, "无结果返回！");
		else
			return new Result(true, list, "操作成功");
	}
	
	/**
	 * 修改角色信息	
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/updateUnderRoles", method = { RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Result updateUnderRoles(Role role){
		int re = 0;
		try {
			re = roleService.updateRoleById(role);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(0 == re)
			return new Result(false, "更新失败");
		else
			return new Result(true, "更新成功");
	}
	
	/**
	 * 增加角色权限
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/addUnderRolesAction", method = { RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Result addUnderRolesAction(List<RoleAction> roleActions){
		int re = roleService.addRoleAuthority(roleActions);
		if(0 == re)
			return new Result(false, "权限赋予失败");
		else
			return new Result(true, "权限赋予成功");
		
	}
	/**
	 * 删除角色权限
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/delUnderRolesAction", method = { RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public Result delUnderRolesAction(List<Integer> list){
		int re = 0;
		try {
			 re=roleService.deleteRoleAction(list);
		} catch (SQLException e) {
		}
		if(0 == re)
			return new Result(false, "角色权限删除失败");
		else
			return new Result(true, "角色权限删除成功");
		
	}
}
